package com.lh.controller;


import cn.hutool.json.JSONObject;
import com.lh.entity.table.FileProperties;
import com.lh.entity.vo.ConfigEditorPlus;
import com.lh.entity.vo.InvestmentProjectBo;
import com.lh.entity.vo.InvestmentProjectVo;
import com.lh.service.IInvestmentProjectService;
import com.lh.utils.Answer;
import com.lh.utils.OSSManageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/richText")
public class RichTextController {
    @Autowired
    private OSSManageUtil oSSManageUtil;

    @Autowired
    private IInvestmentProjectService investmentProjectService;

    @Autowired
    private FileProperties fileProperties;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Answer<Object> add(@Validated @RequestBody InvestmentProjectBo investmentProjectBo) {
        return investmentProjectService.add(investmentProjectBo);
    }


    /**
     * 删除
     */
    @PostMapping("/remove")
    public Answer<Object> remove(@NotEmpty(message = "主键不能为空") @RequestBody List<Long> ids) {
        return investmentProjectService.remove(ids);
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    public Answer<Object> edit(@Validated @RequestBody InvestmentProjectBo investmentProjectBo) {
        return investmentProjectService.edit(investmentProjectBo);
    }

    /**
     * 查询
     */
    @GetMapping("/list")
    public Answer<List<InvestmentProjectVo>> list(@Validated InvestmentProjectBo investmentProjectBo) {
        return investmentProjectService.list(investmentProjectBo);
    }


    /**
     * 富文本插件
     *
     * @return
     */
    @GetMapping("/getConfig")
    public ConfigEditorPlus getConfig() {
        ConfigEditorPlus configEditorPlus = new ConfigEditorPlus();
        return configEditorPlus;
    }


    @PostMapping("/upload")
    public JSONObject fileUpload(@RequestPart("file") MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();

        File dir = new File(fileProperties.getUploadPath());
        if (!dir.exists()) {
            dir.mkdirs();
        }

        File dest = new File(fileProperties.getUploadPath(), originalFilename);
        file.transferTo(dest);

        String url = fileProperties.getUploadPath() + originalFilename;

        JSONObject entries = new JSONObject();
        entries.set("state", "SUCCESS");
        entries.set("url", url);
        entries.set("title", originalFilename);
        entries.set("original", originalFilename);
        return entries;
    }

    /**
     * 根据id查询详情
     */
    @GetMapping("/detail")
    public Answer<Object> detail(@NotEmpty(message = "主键不能为空") @RequestParam Long id) {
        Answer<Object> objectAnswer = investmentProjectService.queryById(id);
        return Answer.ok(objectAnswer);

    }




}
